package com.cookbook.controller;

import com.cookbook.RequestAuthority;
import com.cookbook.Result;
import com.cookbook.database.model.Share;
import com.cookbook.database.model.User;
import com.cookbook.service.ShareService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 毛川
 */
@Controller
@RequestMapping("/share")
@SessionAttributes("user")
public class ShareController {

    @Autowired
    ShareService shareService;

    @Transactional
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public String listShare(
            @RequestParam Integer page,
            @RequestParam Integer limit) {
        if (page < 0 || limit < 0) {
            return Result.format(Result.PARAMETER_INVALID, "非法参数");
        }
        List<Share> shares;
        try {
            shares = shareService.select(page, limit);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
        return Result.success(shares);
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public String insert(
            @ModelAttribute User user,
            @RequestParam String content,
            @RequestParam("images") String images) {
        return insertInternal(user, content, images);
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/inserts", method = RequestMethod.POST)
    @ResponseBody
    public String inserts(
            @ModelAttribute User user,
            @RequestParam String content,
            @RequestParam("images[]") List<String> images) {
        return insertInternal(user, content, String.join(",", images));
    }

    private String insertInternal(User user, String content, String images) {
        if (user == null)
            return Result.format(Result.INTERNAL_ERROR, "内部错误");

        if (content.isEmpty() || content.length() > 255)
            return Result.format(Result.PARAMETER_INVALID, "非法参数");

        Share share = new Share();
        share.setContent(content);
        share.setImages(images);
        share.setUser(user);

        try {
            shareService.insert(share);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
        return Result.success("");
    }

    @RequestAuthority
    @Transactional
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(
            @ModelAttribute User user,
            @RequestParam Integer shareId) {
        if (user == null)
            return Result.format(Result.INTERNAL_ERROR, "内部错误");

        if (shareId < 0)
            return Result.format(Result.PARAMETER_INVALID, "非法参数");

        try {
            Long result = shareService.deleteSelfByShareId(user, shareId);
            if (result == 0)
                return Result.format(Result.NOT_FOUND, "id:" + String.valueOf(shareId) + " 不存在");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.format(Result.INTERNAL_ERROR, "内部错误");
        }
        return Result.success("");
    }
}
